<?php
namespace app\company\controller;
use app\company\model\DocumentTechnology as technology;
use app\company\model\DocumentProject as project;
use app\company\model\DocumentWorking as working;

class Document extends Base
{
	public function index(){

		$type = input('type');

		$document = $this->get_table(1);
		$techlist = $document->order('id DESC')->paginate();
		$document = $this->get_table(2);
		$projectlist = $document->order('id DESC')->paginate();
		$document = $this->get_table(3);
		$workinglist = $document->order('id DESC')->paginate();
		$this->assign('type', $type);
		$this->assign('techlist', $techlist);
		$this->assign('projectlist', $projectlist);
		$this->assign('workinglist', $workinglist);
		return $this->fetch();

	}

	public function add($type){
		if(request()->Ispost()){
				$document = $this->get_table($type);

				$result = $document->allowField(true)
		    ->data(request()->post(), true)
		    ->save();

		    if($result){
		    	$this->success('添加成功',url('index',['type'=>$type]));
		    }else{
		    	$this->error('添加失败');
		    }

		}else{
			return $this->fetch('add_'.$type);
		}
		

	}
	public function edit($type,$id){
		if (request()->isPost()) {
			
			$document = $this->get_table($type);
			$result = $document->isUpdate(true)->save(request()->post());
		    if($result){
		    	$this->success('修改成功',url('index',['type'=>$type]));
		    }else{
		    	$this->error('修改失败');
		    }

		}else{
			$document = $this->get_table($type);
			$item =$document::get($id);
			$this->assign('item',$item);
			return $this->fetch('edit_'.$type);
		}
	}
	public function delete($type,$id){

		$document = $this->get_table($type);
		$result = $document::destroy($id);
		if($result){
			$this->success('删除成功',url('index',['type'=>$type]));
		}else{
			$this->error('删除失败');
		}
	}

	protected function get_table($type){
		switch ($type) {
			case '2':
				$document = new project;
				break;
			case '3':
				$document = new working;
				break;
			default:
				$document = new technology;
				break;
		}

		return $document;
	}
	
	
}